package com.shm.singlecamera.UI;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;


@SuppressLint("AppCompatCustomView")
public class AssistView extends ImageView {
    private RectF ROI; // 记录当前滑动选定的矩形
    private Paint ROIPaint; // 当前滑动选定的矩形的绘制方法
    private RectF ROI1, ROI2;
    private RectF moveTemplate, calibTemplate;
    private boolean isROI = false, isROI1 = false, isROI2 = false;
    private boolean isTemplate = false, isMoveTemplate = false, isCalibTemplate = false;
    private Paint ROI1Paint, ROI2Paint;
    private Paint moveTemplatePaint, calibTemplatePaint;
    private Paint connectedPaint, unconnectedPaint;
    private PointF startPoint;
    private PointF endPoint;

    private final Rect viewRect = new Rect();

    private GestureDetector gestureDetector;

    private final int LINE_WIDTH = 6;

    private boolean connected = false;
    private boolean selected = false;
    private boolean ROILocked = false;
    private boolean templateLocked = false;

    private onViewSelectedListener onViewSelectedListener;

    public void setOnViewSelectedListener(AssistView.onViewSelectedListener onViewSelectedListener) {
        this.onViewSelectedListener = onViewSelectedListener;
    }

    static { System.loadLibrary("miss"); }

    private native void setROI1(float x, float y, float width, float height);
    private native void setROI2(float x, float y, float width, float height);

    private native void setMoveTemplate(float x, float y, float width, float height);
    private native void setCalibTemplate(float x, float y, float width, float height);

    public boolean isROI(){return isROI;}
    public boolean isTemplate(){return isTemplate;}

    private void init() {
        // ROI绘制画笔
        ROIPaint = new Paint();
        ROIPaint.setColor(Color.WHITE);
        ROIPaint.setStyle(Paint.Style.STROKE);
        ROIPaint.setStrokeWidth(LINE_WIDTH);

        // 选中状态画笔
        connectedPaint = new Paint();
        connectedPaint.setColor(Color.GREEN);
        connectedPaint.setAlpha(60);
        connectedPaint.setStyle(Paint.Style.STROKE);
        connectedPaint.setStrokeWidth(8);

        unconnectedPaint = new Paint();
        unconnectedPaint.setColor(Color.RED);
        unconnectedPaint.setAlpha(60);
        unconnectedPaint.setStyle(Paint.Style.STROKE);
        unconnectedPaint.setStrokeWidth(8);

        startPoint = new PointF();
        endPoint = new PointF();
        ROI = new RectF();

        // 手势检测
        gestureDetector = new GestureDetector(getContext(),
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public void onLongPress(MotionEvent e) {
                        handleLongPress(e);
                    }
                });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int viewWidth = getMeasuredWidth();
        int viewHeight = getMeasuredHeight();
        viewRect.set(0, 0, viewWidth, viewHeight);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        gestureDetector.onTouchEvent(event);

        // if(ROILocked) return true;

        if(templateLocked) return true;

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startPoint.set(event.getX(), event.getY());
                endPoint.set(event.getX(), event.getY());
                updateROI();
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                endPoint.set(event.getX(), event.getY());
                updateROI();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                isROI = true;
                isTemplate = true;
                invalidate();
                break;
            default:
                break;
        }
        return true;
    }

    private void updateROI() {
        // 限制ROI在左半屏范围内
        float left = Math.max(viewRect.left, Math.min(startPoint.x, endPoint.x));
        float right = Math.min(viewRect.right, Math.max(startPoint.x, endPoint.x));
        float top = Math.min(startPoint.y, endPoint.y);
        float bottom = Math.max(startPoint.y, endPoint.y);

        ROI.set(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制ROI
        canvas.drawRect(ROI, ROIPaint);

        if(isROI1){
            canvas.drawRect(ROI1, ROI1Paint);
        }

        if(isROI2){
            canvas.drawRect(ROI2, ROI2Paint);
        }

        if(isMoveTemplate){
            canvas.drawRect(moveTemplate, moveTemplatePaint);
        }

        if(isCalibTemplate){
            canvas.drawRect(calibTemplate, calibTemplatePaint);
        }

        // 绘制选中状态
        if (connected) {
            canvas.drawRect(viewRect, connectedPaint);
        }else{
            canvas.drawRect(viewRect, unconnectedPaint);
        }

    }

    public AssistView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AssistView(Context context) {
        super(context);
        init();
    }

    public void cameraConnected() {
        connected = true;
        invalidate();
    }

    public void cameraUnconnected() {
        connected = false;
        invalidate();
    }

    private void handleLongPress(MotionEvent e) {
        int x = (int) e.getX();
        int y = (int) e.getY();

        if (viewRect.contains(x, y)) {
            selected = !selected;
            if(selected){
                onViewSelectedListener.select();
            }else{
                onViewSelectedListener.unselect();
            }

        }
        invalidate();
    }

    public void setROI1() {

        ROI1 = new RectF(ROI);
        ROI1Paint = new Paint();
        ROI1Paint.setColor(ButtonColor.BUTTON_ROI1);
        ROI1Paint.setStyle(Paint.Style.STROKE);
        ROI1Paint.setStrokeWidth(LINE_WIDTH);

        isROI = false;
        isROI1 = true;
        ROI = new RectF();
        invalidate();
    }

    public void setROI2() {

        ROI2 = new RectF(ROI);
        ROI2Paint = new Paint();
        ROI2Paint.setColor(ButtonColor.BUTTON_ROI2);
        ROI2Paint.setStyle(Paint.Style.STROKE);
        ROI2Paint.setStrokeWidth(LINE_WIDTH);

        isROI = false;
        isROI2 = true;
        ROI = new RectF();
        invalidate();
    }

    public void clearROI() {
        isROI = false;
        isROI1 = false;
        isROI2 = false;

        ROI = new RectF();
        invalidate();
    }

    public boolean isROI1() {
        return isROI1;
    }

    public boolean isROI2() {
        return isROI2;
    }

    public void lockROI() {
        // 选定ROI
        setROI1(ROI1.left + LINE_WIDTH / 2.f, ROI1.top + LINE_WIDTH / 2.f,
                ROI1.width() - LINE_WIDTH, ROI1.height() - LINE_WIDTH);
        setROI2(ROI2.left + LINE_WIDTH / 2.f, ROI2.top + LINE_WIDTH / 2.f,
                ROI2.width() - LINE_WIDTH, ROI2.height() - LINE_WIDTH);
        ROILocked = true;
    }

    public void unlockROI() {
        ROILocked = false;
    }

    public void setMoveTemplate() {

        moveTemplate = new RectF(ROI);
        moveTemplatePaint = new Paint();
        moveTemplatePaint.setColor(ButtonColor.BUTTON_MOVE_TEMPLATE);
        moveTemplatePaint.setStyle(Paint.Style.STROKE);
        moveTemplatePaint.setStrokeWidth(LINE_WIDTH);

        isTemplate = false;
        isMoveTemplate = true;
        ROI = new RectF();
        invalidate();
    }

    public void setCalibTemplate() {

        calibTemplate = new RectF(ROI);
        calibTemplatePaint = new Paint();
        calibTemplatePaint.setColor(ButtonColor.BUTTON_CALIB_TEMPLATE);
        calibTemplatePaint.setStyle(Paint.Style.STROKE);
        calibTemplatePaint.setStrokeWidth(LINE_WIDTH);

        isTemplate = false;
        isCalibTemplate = true;
        ROI = new RectF();
        invalidate();
    }

    public void clearTemplate() {
        isTemplate = false;
        isMoveTemplate = false;
        isCalibTemplate = false;

        ROI = new RectF();
        invalidate();
    }

    public boolean isMoveTemplate() {
        return isMoveTemplate;
    }

    public boolean isCalibTemplate() {
        return isCalibTemplate;
    }

    public void lockTemplate() {
        // 选定模板
        // 这里暂时先这样，应该替换为修改模板的逻辑
        setMoveTemplate(moveTemplate.left + LINE_WIDTH / 2.f, moveTemplate.top + LINE_WIDTH / 2.f,
                moveTemplate.width() - LINE_WIDTH, moveTemplate.height() - LINE_WIDTH);
        setCalibTemplate(calibTemplate.left + LINE_WIDTH / 2.f, calibTemplate.top + LINE_WIDTH / 2.f,
                calibTemplate.width() - LINE_WIDTH, calibTemplate.height() - LINE_WIDTH);
        templateLocked = true;
    }

    public void unlockTemplate() {
        templateLocked = false;
    }

    public interface onViewSelectedListener{
        void select();
        void unselect();
    }

}

